const { isLocal } = require('./utils')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

const optimization = {
  runtimeChunk: {
    name: 'manifest'
  },
  splitChunks: {
    chunks: 'all',
    minChunks: 1,
    minSize: 10,  //提取出的chunk的最小大小
    cacheGroups: {
      bundle: {
        name: 'bundle',
        test: (module) => {
          const context = module.context
          if (context && context.includes('ad-ui')) return true
          return false
        },
        priority: 2,
        chunks: 'initial' // 只打包初始时依赖的第三方
      },
      vendors: {
        name: 'vendor',
        test: (module) => {
          const context = module.context
          if (context && context.includes('node_modules') && !context.includes('ad-ui')) return true
          return false
        },
        priority: 2,
        chunks: 'initial'
      }
    }
  },
  minimizer: [
    new UglifyJsPlugin({
      cache: false, // 使用文件缓存，当JS文件没有变化的时候就利用缓存
      parallel: true, // 采用多线程来加速压缩
      sourceMap: true,  // 是否配置source map
    })
  ]
}

module.exports = isLocal ? {} : optimization
